package com.prs.crm.action.project.transmit;

import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.domain.flow.FlowVerifyDetail;
import com.prs.crm.domain.project.Task;
import com.prs.crm.domain.project.TaskTransmit;
import com.prs.crm.exception.InValidException;
import com.prs.crm.service.project.TaskService;
import com.prs.crm.service.project.TaskTransmitService;

public class TaskTransmitAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private TaskService taskService;

	private TaskTransmitService taskTransmitService;

	private FlowVerifyDetail detail;

	private PaginationSupport pagination;

	private File upload;

	private String uploadFileName;

	private TaskTransmit transmit;

	private boolean audited;

	private Task task;

	public void setTaskTransmitService(TaskTransmitService taskTransmitService) {
		this.taskTransmitService = taskTransmitService;
	}

	public TaskTransmitService getTaskTransmitService() {
		return taskTransmitService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public TaskService getTaskService() {
		return taskService;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public FlowVerifyDetail getDetail() {
		return detail;
	}

	public void setDetail(FlowVerifyDetail detail) {
		this.detail = detail;
	}

	public void setAudited(boolean audited) {
		this.audited = audited;
	}

	public boolean isAudited() {
		return audited;
	}

	public void setTask(Task task) {
		this.task = task;
	}

	public Task getTask() {
		return task;
	}

	public File getUpload() {
		return upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public void setTransmit(TaskTransmit transmit) {
		this.transmit = transmit;
	}

	public TaskTransmit getTransmit() {
		return transmit;
	}

	@Action("listTaskForTransmit")
	public String listTaskForTransmit() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				Task.class);
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getTaskService().listExecuteTask(criteria,
				pagination, AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	String a = "";

	@Action("poseTransmit")
	public String poseTransmit() {
		return SUCCESS;
	}

	@Action(value = "createTaskTransmit", results = {
			@Result(name = "success", type = "redirect", location = "listTaskForTransmit"),
			@Result(name = "error", type = "chain", location = "listTaskForTransmit") })
	public String createTaskTransmit() {
		if (transmit.getReciver() == null) {
			
			addActionError("转发人不能为空,重新进行处理");
			return ERROR;
		}
		try {
			this.getTaskTransmitService().createTransmit(transmit, audited);
			this.setPrompt("转发任务成功");
			return SUCCESS;
		} catch (InValidException e) {
			this.setTask(this.getTaskService().get(transmit.getTask().getId()));
			this.addActionError(e.getMessage());
			return ERROR;
		}
	}

	@Action("listVerifyTaskTransmit")
	public String listVerifyTaskTransmit() {
		this.setPagination(this.getTaskTransmitService().listProcessEntitys(
				this.getPaginationHelper().prepare(pagination),
				AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	@Action("verifyTaskTransmit")
	public String verifyTaskTransmit() {
		this.setTask(transmit.getTask());
		return SUCCESS;
	}

	@Clew(results = { "success" })
	@Action(value = "saveTaskTransmitVerify", results = { @Result(name = "success", type = "redirect", location = "listVerifyTaskTransmit") })
	public String saveTaskTransmitVerify() {
		this.getTaskTransmitService().executeVerify(transmit, detail,
				AppContext.getAccountContext().currentUser(), upload,
				uploadFileName);
		this.setPrompt("审核成功");
		return SUCCESS;
	}

	@Action("listTransmitHistory")
	public String listTransmitHistory() {
		this.setPagination(this.getTaskTransmitService().listTransmitHistory(
				this.getPaginationHelper().prepare(pagination),
				AppContext.getAccountContext().currentUser()));
		return SUCCESS;
	}

	@Action("taskTransmitInfo")
	public String taskTransmitInfo() {
		this.setTask(transmit.getTask());
		return SUCCESS;
	}

	@Action("listTaskTransmitVerifyDetail")
	public String listTaskTransmitVerifyDetail() {
		return SUCCESS;
	}

	@Action("viewTaskTransmitVerifyDetail")
	public String viewTaskTransmitVerifyDetail() {
		return SUCCESS;
	}
}
